package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.MessageDAO;
import model.UserDAO;
import beans.MessageBean;
import beans.UserBean;

/**
 * Servlet implementation class DeleteMessageServlet
 */
@WebServlet("/DeleteMessageServlet")
public class DeleteMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteMessageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {	    
			MessageBean message = new MessageBean();
			String ID = request.getParameter("id");
			String box = request.getParameter("box");
			int inbox;
			if (box.equals("sent"))
				inbox = 1;
			else
				inbox = 2;
			message.setCommentID(Integer.parseInt(ID));
			//Debug
			System.out.println(message.getCommentID());
			System.out.println("BOX==" + inbox);
			MessageDAO.deleteMessage(message, inbox);
			//Set the session's attributes
			HttpSession session = request.getSession(true);
			UserBean user = (UserBean)session.getAttribute("currentSessionUser");
			response.sendRedirect("MessagesServlet?id=" + user.getEmail() + "&box=" + box + "&page=1");
			//response.sendRedirect("messages.jsp");      		
		} 


		catch (Throwable theException) 	    
		{
			System.out.println(theException); 
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
